import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class FormPhotoWidget extends StatefulWidget {
  final VoidCallback? onTap;

  String? tips;

  String? path;

  FormPhotoWidget({super.key, required this.onTap, this.path, this.tips = '点击上传照片'});

  @override
  State<FormPhotoWidget> createState() => _FormPhotoWidgetState();
}

class _FormPhotoWidgetState extends State<FormPhotoWidget> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: widget.onTap,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.circular(50),
            child: Container(
              width: 100,
              height: 100,
              decoration: BoxDecoration(
                color: widget.path != null && widget.path!.isNotEmpty ? Colors.white : Colors.grey,
                shape: BoxShape.circle,
              ),
              child: widget.path != null && widget.path!.isNotEmpty
                  ? CachedNetworkImage(
                      imageUrl: widget.path!,
                      fit: BoxFit.cover,
                      errorWidget: (context, url, error) => const Icon(Icons.error),
                      progressIndicatorBuilder: (context, url, progress) => Center(
                        child: CircularProgressIndicator(
                          value: progress.progress,
                        ),
                      ),
                    )
                  : Icon(
                      Icons.camera_alt,
                      size: 50,
                      color: Colors.white,
                    ),
            ),
          ),
          Text(widget.tips ?? ''),
        ],
      ),
    );
  }
}
